﻿using IdentityService.Domain.Repositories;
using IdentityService.Domian.Repositories;
using MediatR;

namespace IdentityService.Application.Commands
{
    public record RemoveMemberCommand(long roleId, IList<long> userIds) : IRequest<bool>;

    public class RemoveMemberCommandHandler : IRequestHandler<RemoveMemberCommand, bool>
    {
        private readonly IRoleRepository _roleRepository;
        private readonly IUserAccountRepository _userAccountRepository;
        public RemoveMemberCommandHandler(IRoleRepository roleRepository, IUserAccountRepository userAccountRepository)
            => (_roleRepository, _userAccountRepository) = (roleRepository, userAccountRepository);

        public async Task<bool> Handle(RemoveMemberCommand request, CancellationToken cancellationToken)
        {
            var role = await _roleRepository.FindByIdIncludeUserAccountsAsync(request.roleId);
            var accounts = await _userAccountRepository.FindByIdBlukAsync(request.userIds);

            if (role != null && accounts.Count > 0)
                role.RemoveUserAccountRange(accounts);

            return true;
        }
    }

}
